﻿namespace Socut
{
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Web;

    internal class CAcc
    {
        public OleDbConnection ConnAcc;
        public string DataAcc = CLink.Source;

        public void Close()
        {
            if (this.ConnAcc.State == ConnectionState.Open)
            {
                this.ConnAcc.Close();
                this.ConnAcc.Dispose();
                this.ConnAcc = null;
            }
        }

        public DataSet ExecuteDataSet(string StoreName, int startindex, int num, string dataname, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            DataSet dataSet = new DataSet();
            OleDbCommand cmd = new OleDbCommand(StoreName, this.ConnAcc);
            cmd.CommandTimeout = 0;
            CParameter.AccessParameters(StoreName, para, cmd, this.ConnAcc);
            new OleDbDataAdapter(cmd).Fill(dataSet, startindex, num, dataname);
            this.Close();
            return dataSet;
        }

        public int ExecuteNonQuery(string sql, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            OleDbCommand cmd = new OleDbCommand(sql, this.ConnAcc);
            cmd.CommandTimeout = 0;
            CParameter.AccessParameters(sql, para, cmd, this.ConnAcc);
            int num = cmd.ExecuteNonQuery();
            this.Close();
            return num;
        }

        public object ExecuteScalar(string SQL, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            OleDbCommand cmd = new OleDbCommand(SQL, this.ConnAcc);
            cmd.CommandTimeout = 0;
            CParameter.AccessParameters(SQL, para, cmd, this.ConnAcc);
            object obj2 = cmd.ExecuteScalar();
            this.Close();
            return obj2;
        }

        public void Open(int ConnIndex)
        {
            string dataAcc = this.DataAcc;
            if (dataAcc.IndexOf("|") > -1)
            {
                dataAcc = dataAcc.Split(new char[] { '|' })[ConnIndex];
            }
            if (this.DataAcc.IndexOf(":") < 0)
            {
                dataAcc = HttpContext.Current.Server.MapPath(dataAcc);
            }
            dataAcc = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataAcc + ";";
            this.ConnAcc = new OleDbConnection(dataAcc);
            if (this.ConnAcc.State == ConnectionState.Closed)
            {
                this.ConnAcc.Open();
            }
        }
    }
}

